package org.softmed.rest.admin.module;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputMethodEvent;
import java.awt.event.InputMethodListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import org.softmed.rest.admin.interfaces.EventListener;
import org.softmed.rest.admin.interfaces.PathChangedListener;
import org.softmed.rest.admin.interfaces.RemoveListener;
import org.softmed.swing.IconManager;

import restlet.description.convert.RESTModuleDescription;


public class ModuleHeader extends JPanel implements ActionListener,
		KeyListener, DocumentListener {

	JTextField name = new JTextField();
	JTextField path = new JTextField();
	RemoveListener removeListener;
	ModuleActionPanel actionPanel = new ModuleActionPanel();
	PathChangedListener pathChangedListener;
	JButton remove = new JButton(IconManager.remove);

	int width = 20;
	int height = 20;

	private RESTModuleDescription moduleDescription;

	public ModuleHeader() {

		name.addKeyListener(this);
		path.addKeyListener(this);
		path.getDocument().addDocumentListener(this);
		name.getDocument().addDocumentListener(this);

		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

		setPreferredSize(new Dimension(540, 30));
		setMaximumSize(new Dimension(540, 30));
		setMinimumSize(new Dimension(540, 30));

		// setBorder(BorderFactory.createLineBorder(Color.BLACK));

		JLabel label = new JLabel("Module");
		label.setPreferredSize(new Dimension(50, 25));
		label.setMaximumSize(new Dimension(50, 25));
		label.setMinimumSize(new Dimension(50, 25));

		JLabel pathLabel = new JLabel("Path");
		pathLabel.setPreferredSize(new Dimension(30, 25));
		pathLabel.setMaximumSize(new Dimension(30, 25));
		pathLabel.setMinimumSize(new Dimension(30, 25));

		path.setPreferredSize(new Dimension(340, 25));
		path.setMaximumSize(new Dimension(340, 25));
		path.setMinimumSize(new Dimension(340, 25));

		name.setPreferredSize(new Dimension(100, 25));
		name.setMaximumSize(new Dimension(100, 25));
		name.setMinimumSize(new Dimension(100, 25));

		add(label);
		add(name);
		add(pathLabel);
		add(path);
		addButton(remove);

		add(Box.createHorizontalGlue());

		revalidate();
		repaint();

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		Object source = arg0.getSource();
		// System.out.println("Action Event !!!!");
		//
		// if (source == add) {
		// addResourceListener.listen(this);
		// } else
		if (source == remove) {
			removeListener.remove(this);
		}

	}

	public void validateData() throws ValidationException {
		if (name.getText() == null || name.getText().length() == 0) {
			throw new ValidationException("Module Name can't be empty !!! ");
		} else if (path.getText() == null || path.getText().length() == 0) {
			throw new ValidationException("Module path can't be empty !!! ");
		}

	}

	@Override
	public void keyPressed(KeyEvent arg0) {

	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// System.out.println("Action Event !!!!");
		Object source = arg0.getSource();
		if (source == name) {
			moduleDescription.setName(name.getText());
			// TODO trigger changes
			return;
		}

		if (source == path) {
			moduleDescription.setPath(path.getText());
			// TODO trigger changes
		}

	}

	@Override
	public void keyTyped(KeyEvent arg0) {

	}

	public void setModuleDescription(RESTModuleDescription moduleDescription) {
		this.moduleDescription = moduleDescription;
		name.setText(moduleDescription.getName());
		path.setText(moduleDescription.getPath());

	}

	protected void addButton(JButton button) {
		resizeButton(button, width, height);
		button.addActionListener(this);
		// button.setEnabled(false);
		add(button);
	}

	protected void resizeButton(JButton button, int width, int height) {
		button.setPreferredSize(new Dimension(width, height));
		button.setMaximumSize(new Dimension(width, height));
		button.setMinimumSize(new Dimension(width, height));
	}

	public RemoveListener getRemoveListener() {
		return removeListener;
	}

	public void setRemoveListener(RemoveListener removeListener) {
		this.removeListener = removeListener;
	}

	protected void notifyChange() {
		pathChangedListener.pathChanged(path.getText());
	}

	@Override
	public void changedUpdate(DocumentEvent arg0) {
		notifyChange();
		if (arg0.getDocument() == name.getDocument())
			moduleDescription.setName(name.getText());
		else
			moduleDescription.setPath(path.getText());
	}

	@Override
	public void insertUpdate(DocumentEvent arg0) {
		notifyChange();
		if (arg0.getDocument() == name.getDocument())
			moduleDescription.setName(name.getText());
		else
			moduleDescription.setPath(path.getText());

	}

	@Override
	public void removeUpdate(DocumentEvent arg0) {
		notifyChange();
		if (arg0.getDocument() == name.getDocument())
			moduleDescription.setName(name.getText());
		else
			moduleDescription.setPath(path.getText());

	}

	public PathChangedListener getPathChangedListener() {
		return pathChangedListener;
	}

	public void setPathChangedListener(PathChangedListener pathChangedListener) {
		this.pathChangedListener = pathChangedListener;
	}

}
